package com.imay.live.fragment.pay;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.imay.base.fragment.BaseFragment;
import com.imay.live.adapter.GetMonenyLinearAdapter;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.model.WithdrawModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.view.custom.LoadRefreshViewControl;
import com.imay.live.view.recyclerview.WrapContentLinearLayoutManager;
import com.imay.live.R;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

/**
 * Created by lzx on 2017/04/17 0017.
 * 提现记录
 */

public class GetthistoryFrament extends BaseFragment {
    private RecyclerView mrecyclerView;
    ArrayList<MultiItemEntity> mdatas;
    GetMonenyLinearAdapter mAdapter;
    FrameLayout container;
    LoadRefreshViewControl loadViewControl;

    public static GetthistoryFrament newInstance() {
        return new GetthistoryFrament();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.payhistory_layout;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        container = $(R.id.container);
        mdatas = new ArrayList<MultiItemEntity>();
        mAdapter = new GetMonenyLinearAdapter(mdatas);
        mAdapter.addFooterView(new View(getActivity()));
        loadViewControl = new LoadRefreshViewControl(getContext(), mAdapter, 20);
        mrecyclerView = loadViewControl.addLoadView(container, new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                refreshLoad();
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                moreLoad(loadViewControl.getStart());
            }
        });
        mrecyclerView.setAdapter(mAdapter);
        WrapContentLinearLayoutManager wrapContentLinearLayoutManager
                = new WrapContentLinearLayoutManager(getActivity());
        mrecyclerView.setLayoutManager(wrapContentLinearLayoutManager);
        mrecyclerView.setHasFixedSize(true);
        mrecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .size(4)
                .build());
        refreshLoad();
        MobclickAgent.onEvent(getActivity(),"bill_cash_open");

    }

    private void refreshLoad() {
        ProtoRequestHelper.task(ProtoHttpFactory.getwithdrawList(0, loadViewControl.getDATACOUNT())).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                try {
                    Result<ArrayList<WithdrawModel>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<WithdrawModel>>>() {
                    }.getType());
                    loadViewControl.refreshLoadView(result.data, result.result);

                } catch (Exception e) {
                    loadViewControl.loadError();
                }
            }

            @Override
            public void onRequestError(String command, int code, String message) {
                loadViewControl.loadError(message);
            }
        });
    }

    private void moreLoad(int nstart) {
        ProtoRequestHelper.task(ProtoHttpFactory.getwithdrawList(nstart, loadViewControl.getDATACOUNT())).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                try {
                    Result<ArrayList<WithdrawModel>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<WithdrawModel>>>() {
                    }.getType());
                    loadViewControl.moreLoadView(result.data, result.result);

                } catch (Exception e) {
                    loadViewControl.loadError();
                }
            }

            @Override
            public void onRequestError(String command, int code, String message) {
                loadViewControl.loadError(message);

            }
        });
    }
    boolean isCreated;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCreated = true;
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        // 友盟统计页面跳转，重写此方法，可以保证ViewPager切换Fragment时能够准确的记录Fragment之间的跳转
        // 不用再调用Fragment的生命周期方法
        if (isCreated){
            if (isVisibleToUser) {
                MobclickAgent.onPageStart(getClass().getSimpleName());
            } else {
                MobclickAgent.onPageEnd(getClass().getSimpleName());
            }
        }
    }
}
